package com.jadefashion.controller;

import com.jadefashion.health.entity.PageResult;
import com.jadefashion.health.entity.QueryPageBean;
import com.jadefashion.health.entity.Result;
import com.jadefashion.health.resources.MessageConstant;
import com.jadefashion.pojo.Permission;
import com.jadefashion.service.PermissionService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RequestMapping("/permission")
@RestController
public class PermissionController {

    @Reference
    private PermissionService permissionService;


    //将json数据封装进实体类
    //TODO: 分页查询
    @RequestMapping("/queryPage")
    public Result queryPage(@RequestBody QueryPageBean queryPageBean) {
        //System.out.println(queryPageBean);
        try {
            //根据实体属性也就是参数查询数据
            PageResult pageResult = permissionService.queryPage(queryPageBean.getQueryString(), queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            //封装返回Result,包括flag,信息,数据
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    //TODO: 添加权限
    @RequestMapping("/addPermission")
    public Result addPermission(@RequestBody Permission permission) {

        try {
            permissionService.addPermission(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    //TODO:    删除权限
    @RequestMapping("/deletePermission")
    public Result deletePermission(@RequestBody HashMap<String, Object> map) {

        try {
            Integer id = (Integer) map.get("id");
            permissionService.deletePermission(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    //TODO:   编辑表单权限
    @RequestMapping("/editPermission")
    public Result editPermission(@RequestBody Permission permission) {
        try {
            permissionService.editPermission(permission);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }

    //TODO:   查询所有permission
    @RequestMapping("/queryAllPermission")
    public Result queryAllPermission() {
        try {
            List<Permission> list_permission = permissionService.queryAllPermission();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, list_permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    //TODO: 查询role对应permission
    @RequestMapping("/queryPermissionForRole")
    public Result queryPermissionForRole(@RequestBody HashMap<String, String> map) {

        try {
            Integer id = Integer.parseInt(map.get("id"));
            List<Permission> list_permission = permissionService.queryPermissionForRole(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, list_permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }
}
